<template>
  <div class="container">
    <div class="basepadding">报修记录</div>
    <div class="basepadding">
      <div class="basepadding" style="background: rgba(255, 255, 255, 0.1)">
        <z-from
          ref="searchfrom"
          :inline="true"
          label-position="left"
          label-width="100px"
          :nowinputs="RepRecordsearchinputs"
          size="small"
        >
          <template #zfromafter>
            <el-form-item style="margin: 0">
              <el-button type="primary" @click="searchfn()">搜索</el-button>
              <el-button @click="deletesearchfn()">清空</el-button>
            </el-form-item>
          </template>
        </z-from>
      </div>
    </div>
    <div class="basepadding">
      <el-button type="primary" @click="RepRecordaddfn()">添加</el-button>
    </div>
    <div class="basepadding">
      <z-table
        v-loading="tableloading"
        :datas="RepRecord"
        :tablecolumn="tablecolumn"
        @delfn="delfn"
      >
        <template #setting="setting">
          <el-table-column
            align="center"
            :label="setting.itemdata.label"
            :prop="setting.itemdata.value"
          >
            <template #default="scope">
              <el-button
                size="small"
                type="primary"
                @click="RepRecordaddfn(scope.row)"
              >
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </z-table>
    </div>

    <el-dialog
      v-model="dialogfalg"
      :close-on-click-modal="false"
      :title="dialogtitle"
      width="800px"
      @closed="closedfn"
      @opened="openfn"
    >
      <z-from
        ref="from"
        label-position="left"
        label-width="100px"
        :nowdata="nowdata"
        :nowinputs="nowinputs"
      >
        <template #gtel="scope">
          <el-form-item :label="scope.item.label" :prop="scope.item.value">
            <el-input
              v-model="scope.formdata[scope.item.value]"
              @blur="gtelblur(scope.formdata[scope.item.value])"
            />
          </el-form-item>
        </template>
      </z-from>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogfalg = false">取 消</el-button>
          <el-button
            :loading="edtbtnloading"
            type="primary"
            @click="updatafn()"
          >
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
  import {
    RepRecordaddinputs,
    RepRecordedtinputs,
    RepRecordsearchinputs,
    tablecolumn,
  } from '@/json/RepRecord.js'
  import zFrom from '../../components/zFrom/zFrom'
  import zTable from '../../components/zTable/index'

  export default {
    name: 'RepRecord',
    components: {
      zFrom,
      zTable,
    },
    data() {
      return {
        dialogfalg: false,
        edtbtnloading: false,
        nowdata: {},
        nowinputs: {},
        dialogtitle: '',
        RepRecord: [],
        updatas: [],
        tablecolumn,
        tableloading: false,
        RepRecordsearchinputs,
      }
    },
    mounted() {
      this.getdata()
    },
    methods: {
      searchfn() {
        this.$refs.searchfrom.validate().then((updata) => {
          this.RepRecord = this.updatas.filter((e) => {
            for (let i = 0; i < this.RepRecordsearchinputs.length; i++) {
              let dqinput = this.RepRecordsearchinputs[i],
                v =
                  e[dqinput.value] || e[dqinput.value] == 0
                    ? e[dqinput.value]
                    : ''
              if (dqinput.type == 'select') {
                if (
                  updata[dqinput.value] &&
                  updata[dqinput.value] != '' &&
                  updata[dqinput.value] != v
                ) {
                  return false
                }
              } else {
                if (
                  updata[dqinput.value] &&
                  updata[dqinput.value] != '' &&
                  v.indexOf(updata[dqinput.value]) < 0
                ) {
                  return false
                }
              }
            }
            return true
          })
        })
      },
      deletesearchfn() {
        this.searchdata = {}
        this.$refs.searchfrom.settingfn()
        this.RepRecord = this.$z.deepClone(this.updatas)
      },
      async getdata() {
        this.tableloading = true
        let data = await this.$z.$axios.post('/RepRecord')
        this.updatas = data.data
        this.RepRecord = data.data
        this.tableloading = false
      },
      RepRecordaddfn(scope = {}) {
        let data = scope[Symbol.for('data')]
        this.dialogfalg = true
        this.dialogtitle = data ? '修改' : '添加'
        let btns = data ? RepRecordedtinputs : RepRecordaddinputs
        this.nowinputs = [...btns]
        this.nowdata = data ? data : {}
      },
      openfn() {},
      closedfn() {},
      async updatafn() {
        let updata = await this.$refs.from.validate()
        console.log(updata)
        this.edtbtnloading = true
        let url = updata.id ? '/RepRecordedt' : '/RepRecordadd'
        let { data } = await this.$z.$axios.post(url, updata).catch(() => {
          this.edtbtnloading = false
        })
        let par = this.$z.parfn(data)
        if (par.value == 'succeed') {
          this.$message.success(par.label)
          this.edtdialogfalg = false
          this.getdata()
        } else {
          this.$message.error(par.label)
        }
        this.edtbtnloading = false
        this.dialogfalg = false
      },
      async gtelblur(gtel) {
        if (!gtel || gtel == '') {
          return
        }
        let { data } = await this.$z.$axios.post('/ChargeCar', { gtel })
        if (data.length == 1) {
          this.$refs.from.forcesetformdatafn({ mc: data[0].mc })
          let Stationdata = await this.$z.$axios.post('/Station', {
            id: data[0].pcs,
          })
          data = Stationdata.data
          if (data.length == '1') {
            this.$refs.from.forcesetformdatafn({ pcs: data[0].pcs })
          }
        }
      },
      async delfn(datas = []) {
        datas.forEach(async (e) => {
          await this.$z.$axios.post('/RepRecorddel', { id: e.id })
        })
        this.getdata()
      },
    },
  }
</script>
<style lang="scss" scoped>
  @import '@/styles/tablecss.scss';
</style>
